/*
 * Created by JFormDesigner on Mon Jul 13 22:02:28 CST 2020
 */

package entity.JPanel;

import entity.Model.User;
import entity.Model.Words;
import utils.JDBCUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class resetpassword extends JPanel {
    BufferedImage bg;
    private int width;
    private int height;
    //数据库参数
    private PreparedStatement preparedStatement;
    private int result = 0;
    private ResultSet rs;
    private Connection con = null;
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JTextField textField2;
    private JButton button3;
    private JLabel words;
    private String Jlabel_words[];
    private Random random = new Random();
    List<Words> list = new ArrayList<>();

    public resetpassword(int width, int height) {
        this.width = width;
        this.height = height;
        setLayout(null);
        initComponents();
        try {
            bg = ImageIO.read(UserLoginJPanel.class.getClassLoader().getResourceAsStream("resources/images/图书.jpg"));
        } catch (IOException e) {
            System.out.println("NULL");
        }
    }

    public void paintComponent(Graphics g) {
        g.drawImage(bg, 0, 0, width, height, null);//绘制背景图片
    }

    private void initComponents() {
        //--------label1 ----------
        label1 = new JLabel();
        label1.setText("\u8d26\u53f7:");
        label1.setForeground(new Color(248, 248, 255));
        label1.setFont(new Font("\u7b49\u7ebf", Font.BOLD, 25));
        add(label1);
        label1.setBounds(790, 200, 80, 70);
        System.out.println(width);
        //---------label2 ----------
        label2 = new JLabel();
        label2.setText("\u5bc6\u7801:");
        label2.setForeground(new Color(248, 248, 255));
        label2.setFont(new Font("\u7b49\u7ebf", Font.BOLD, 25));
        add(label2);
        label2.setBounds(790, 300, 80, 70);
        //---------textField1---------
        textField1 = new JTextField();
        textField1.setText(User.getName());
        textField1.setBounds(850, 215, 200, 40);
        textField1.setFont(new Font("宋体", Font.BOLD, 20));
        add(textField1);
        //---------textField2---------
        textField2 = new JTextField();
        textField2.setFont(new Font("宋体", Font.BOLD, 20));
        add(textField2);
        textField2.setBounds(850, 315, 200, 40);
        //----------words-------------
        words = new JLabel();
        words.setBounds(550, 800, 1200, 30);
        words.setFont(new Font("楷体", Font.BOLD | Font.ITALIC, 25));
        words.setForeground(new Color(0, 255, 255));
        returnwords();
        words.setText(Jlabel_words[random.nextInt(list.size())]);
        add(words);
        //----------button3 ----------
        button3 = new JButton();
        button3.setText("submit");
        button3.setFont(new Font("\u65b0\u5b8b\u4f53", Font.ITALIC, 18));
        button3.setBackground(new Color(255, 160, 122));
        button3.setBorderPainted(false);
        button3.setFocusPainted(false);
        button3.setIcon(new ImageIcon(UserLoginJPanel.class.getClassLoader().getResource("resources/images/icon.png").getPath()));
        button3.setBounds(800, 400, 251, 47);
        add(button3);
        button3.addActionListener((e) -> {
            if (textField2.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "密码不能为空", "success", JOptionPane.WARNING_MESSAGE);
            } else {
                try {
                    con = JDBCUtil.getConnect();
                    String sql = "update users_table set password=? where name=?";
                    System.out.println("con+" + con);
                    preparedStatement = con.prepareStatement(sql);
                    preparedStatement.setString(1, textField2.getText());
                    preparedStatement.setString(2, User.getName());
                    result = preparedStatement.executeUpdate();
                    System.out.println("修改1条数据");
                    JOptionPane.showMessageDialog(null, "修改密码成功", "success", JOptionPane.WARNING_MESSAGE);
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        });
        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for (int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
    }

    public void returnwords() {
        try {
            con = JDBCUtil.getConnect();
            String sql = "select * from words";
            preparedStatement = con.prepareStatement(sql);
            rs = preparedStatement.executeQuery();
            while (rs.next()) {
                list.add(new Words(rs.getString("words"), rs.getString("Author")));
            }
            Jlabel_words = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                Jlabel_words[i] = list.get(i).getWords();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(preparedStatement, rs, con);
            System.out.println("密码修改数据库连接结束");
        }
    }
}
